<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Staffmodel;

use app\index\controller\Basecontroller;



class Staff extends Basecontroller
{
  protected $model = null;
  function __construct()
  {
    parent::__construct();
    $this->model = new Staffmodel();
  }


  // 后台员工登录功能
  function adminLogin()
  {

    //取到post数据
    $data = getPost();
    // 查询值是否存在
    $res = Staffmodel::where($data)->find();
    if (!$res) {
      return res(1001, '账号密码错误');
    }
    // 创造一个token
    $token = create_token();
    // 存入redis
    $this->redis->set($token, json_encode($res));

    // 返回token值
    return res(1000, 'ok', [
      'token' => $token
    ]);
  }
  public function selects2()
  {
    $result1 = db()->query("SELECT gjc_job.name ,gjc_job.jid FROM gjc_job");
    return res(1001, '拿到单独的职业id和职业名称', $result1);
  }
  //拿到员工ID
  public function Getposionid()
  {
    // $nowjson = json_decode(file_get_contents("php://input"), true);

    $result = db()->query("SELECT gjc_job.name ,gjc_job.jid FROM gjc_job");
    return res(1000, '拿到职业id成功', $result);
  }
  // 员工添加
  public function add()
  {
    $nowjson = getPost();

    $where = [
      'acc' => $nowjson['Acc']
    ];

    $res = db('staff')->where($where)->find();
    if ($res) {
      return res(1001, '账号存在重复');
    }
    $data = [
      'acc' => $nowjson['Acc'],
      'pwd' => $nowjson['password'],
      'nickname' => $nowjson['UserName'],
      'job_id' => $nowjson['position'],
      'acc' => $nowjson['Acc'],
      'create_time'=>date('y-m-d H:i:s' ,time())
    ];
    $res = db('staff')->insert($data);
    return res(1001, '添加成功',$res );


  }

  // 员工查询
  public static function select()
  { //这个static也可以去除掉，效果是一样的
    $nowIDjson = json_decode(file_get_contents("php://input"), true);

    if (!empty($nowIDjson)) {
      $search = $nowIDjson['serchdata']; //搜索内容
      $result = db()->query("select e.* ,g.* from gjc_staff e , gjc_job g WHERE e.job_id = g.jid and e.nickname  LIKE '%$search%'");
      return json($result);
      exit;
    } else {
      $result = db()->query("select e.* ,g.* from gjc_staff e , gjc_job g WHERE e.job_id = g.jid");
      return json($result);
    }


  }
  //员工修改功能
  public function edit()
  {
    $nowIDjson = json_decode(file_get_contents("php://input"), true);

    $id = $nowIDjson['id']; //id
    $nickname = $nowIDjson['nickname']; //名字
    // $acc = $nowIDjson['acc']; //账号
    $pwd = $nowIDjson['pwd']; //密码
    $position = $nowIDjson['position'];


    $result = db()->query(" update gjc_staff ,gjc_job SET gjc_staff.pwd ='$pwd',gjc_staff.nickname = '$nickname ',  gjc_staff.job_id = '$position' WHERE gjc_staff.staff_id =' $id' AND gjc_staff.job_id = gjc_job.jid ;");
    return res(1000, '修改成功', $result);
  }
  public function del()
  {
    $nowIDjson = json_decode(file_get_contents("php://input"), true);
    $id = $nowIDjson['id']; //id


    $result = db()->query("DELETE from gjc_staff WHERE staff_id = '$id'");
    return res(1001, '删除成功', $result);
  }
  //员工禁用启用功能
  public function stateValue()
  {
    $nowIDjson = json_decode(file_get_contents("php://input"), true);
    $Btntypedata2 = ""; //一个空的变量，用来转换状态
    $id = $nowIDjson['id']; //id
    $Btntypedata = $nowIDjson['Btntypedata']; //本来传过来的状态
    if ($Btntypedata == '启用') {
      $Btntypedata = '禁用';
      $Btntypedata2 = $Btntypedata;
      $where = [
        'staff_id'  =>  $id

      ];
      $upada = [
        'state' =>  $Btntypedata2
      ];
      $result = db('staff')->where($where)->update($upada);
    } else if ($Btntypedata == '禁用') {
      $Btntypedata = '启用';
      $Btntypedata2 = $Btntypedata;
      $where = [
        'staff_id'  =>  $id

      ];
      $upada = [
        'state' =>  $Btntypedata2
      ];
      $result = db('staff')->where($where)->update($upada);
      // $result2 = db('staff')->where( $where)->find();

    }

    return res(1000, '操作成功， 此员工状态已改变', $result);
  }
 
  function getLogin()
  {
    $user = $this->verifyLogin();

    if (!$user) {
      return $this->notLogin();
    }
    // 根据用户id来获得权限id
    // $where

    $res = db('job')
      ->where("jid = '{$user['job_id']}'")
      ->field('menu_id')
      ->find();




    $data = [
      'nickname' => $user['nickname'],
      'mids' => $res['menu_id'],
      'create_time'=>$user['create_time'],
      'acc'=>$user['acc'],
      'job_name'=>$user['job_name'],
    ];
    // return json($data);
    return res(1000, 'ok', $data);
  }

  function logout()
  {
    $token = getheaders()['Access-Token'];
    $this->redis->del($token);
    return res(1000, 'ok');
  }
  function getStaffLog()
  {
    $token = !empty(getheaders()['Access-Token']) ? getheaders()['Access-Token'] : null;

    if (!$token) {
      return res(1011, '服务器繁忙，请稍后重试');
    }
    // 判断是否为员工的身份
    $tokenData = json_decode($this->redis->get($token), true);
    if (!$tokenData) {
      return res(1011, '服务器繁忙，请稍后重试');
    }
    $res = $this->model->getChatLog();


    return res(1000, 'ok', $res);
  }


}
